package com.road.yishi;

import com.road.yishi.config.GatewayConfig;
import com.road.yishi.conn.ConnMgr;

/**
 * @Description :
 * @FileName: GatewayServer.java
 * @Author :WeiHui.Zhang
 * @Data : 2016年2月26日 下午2:18:08
 * @Version:V1.00
 */
public final class GatewayServer extends BaseServer<GatewayConfig> {

	private static GatewayServer gatewayServer;

	private static GatewayConfig gatewayConfig;
	
	private ConnMgr connMgr;

	public static GatewayServer getInstance() {
		return Singleton.instance;
	}

	private final static class Singleton {

		private static GatewayServer instance = new GatewayServer();
	}

	@Override
	public boolean start() {
		if (!super.start()) {
			return false;
		}
		
		if (!initConnector()) {
			return false;
		}
		
		return true;
	}

	private boolean initConnector() {
		connMgr = new ConnMgr();
		if (!initComponent(connMgr.init(getServerId()), "启动gateway连接Server socket服务")) {
			return false;
		}
//		startAdminCmdMgr();
		return true;
	}
	
	@Override
	public boolean stop() {
		return super.stop();
	}

	public static void main(String[] args) {
		long time = System.currentTimeMillis();

		gatewayServer = GatewayServer.getInstance();
		gatewayConfig = gatewayServer.getConfig();

		System.out.println(gatewayConfig.crossPath());
		System.out.println(gatewayConfig.globalPath());
		System.out.println(gatewayConfig.serverId());

		if (!gatewayServer.start()) {
			System.err.println("GatewayServer启动失败!");
			System.exit(1);
			return;
		}

		LOGGER.info("GatewayServer启动成功! 启动耗时: " + (System.currentTimeMillis() - time));
	}

	@Override
	protected String getGlobalFilePath() {
		return gatewayConfig.globalPath();
	}

	@Override
	protected String getCrossFilePath() {
		return gatewayConfig.crossPath();
	}

	@Override
	protected String getScanPackage() {
		return null;
	}

	@Override
	protected int getServerId() {
		return gatewayConfig.serverId();
	}

}
